package com.example.game_forum.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.game_forum.dto.TPostContentDto;
import com.example.game_forum.dto.TPostDto;
import com.example.game_forum.dto.TPostTitleDto;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface TPostRepository extends BaseMapper<TPostDto> {

    /**
     * 根据板块查询帖子目录
     * @param postType 帖子类型
     * @return 帖子标题列表
     */
    @Select("SELECT p.post_id, p.title, p.like_count, p.comment_count " +
            "FROM t_post p " +
            "WHERE p.post_type = #{postType} " +
            "ORDER BY p.create_time DESC")
    List<TPostTitleDto> selectPostTitlesByPostType(@Param("postType") String postType);

    /**
     * 根据标题模糊搜索帖子
     * @param title 标题关键词
     * @return 帖子标题列表
     */
    @Select("SELECT p.post_id, p.title, p.like_count, p.comment_count " +
            "FROM t_post p " +
            "WHERE p.title LIKE CONCAT('%', #{title}, '%') " +
            "ORDER BY p.create_time DESC")
    List<TPostTitleDto> selectPostTitlesByTitle(@Param("title") String title);

    /**
     * 根据用户ID搜索帖子
     * @param userId 用户ID
     * @return 帖子标题列表
     */
    @Select("SELECT p.post_id, p.title, p.like_count, p.comment_count " +
            "FROM t_post p " +
            "WHERE p.user_id = #{userId} " +
            "ORDER BY p.create_time DESC")
    List<TPostTitleDto> selectPostTitlesByUserid(@Param("userId") String userId);

    /**
     * 根据帖子ID查询帖子详情
     * @param postId 帖子ID
     * @return 帖子详情
     */
    @Select("SELECT p.post_id, p.title, p.content, p.post_type, p.category, " +
            "p.user_id, p.view_count, p.like_count, p.comment_count, " +
            "p.is_top, p.is_good, p.image_urls, p.video_url, p.tags, " +
            "p.create_time, p.update_time, " +
            "u.username, u.avatar, u.gender, u.level " +
            "FROM t_post p " +
            "LEFT JOIN t_user u ON p.user_id = u.user_id " +
            "WHERE p.post_id = #{postId}")
    @Results({
            @Result(property = "postId", column = "post_id"),
            @Result(property = "title", column = "title"),
            @Result(property = "content", column = "content"),
            @Result(property = "postType", column = "post_type"),
            @Result(property = "category", column = "category"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "viewCount", column = "view_count"),
            @Result(property = "likeCount", column = "like_count"),
            @Result(property = "commentCount", column = "comment_count"),
            @Result(property = "isTop", column = "is_top"),
            @Result(property = "isGood", column = "is_good"),
            @Result(property = "imageUrls", column = "image_urls"),
            @Result(property = "videoUrl", column = "video_url"),
            @Result(property = "tags", column = "tags"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time"),
            @Result(property = "userName", column = "username"),
            @Result(property = "userAvatar", column = "avatar"),
            @Result(property = "userGender", column = "gender"),
            @Result(property = "userLevel", column = "level")
    })
    TPostContentDto selectPostContentById(@Param("postId") String postId);

    /**
     * 发布新帖子
     * @param postDto 帖子信息
     */
    @Insert("INSERT INTO t_post (title, content, post_type, category, user_id, image_urls, video_url, tags) " +
            "VALUES (#{title}, #{content}, #{postType}, #{category}, #{userId}, #{imageUrls}, #{videoUrl}, #{tags})")
    @Options(useGeneratedKeys = true, keyProperty = "postId")
    void insertPost(TPostDto postDto);

    /**
     * 更新帖子信息
     * @param postId 帖子ID
     * @param postDto 更新的帖子信息
     */
    @Update("UPDATE t_post SET " +
            "title = #{postDto.title}, " +
            "content = #{postDto.content}, " +
            "post_type = #{postDto.postType}, " +
            "category = #{postDto.category}, " +
            "image_urls = #{postDto.imageUrls}, " +
            "video_url = #{postDto.videoUrl}, " +
            "tags = #{postDto.tags}, " +
            "update_time = CURRENT_TIMESTAMP " +
            "WHERE post_id = #{postId}")
    void updatePost(@Param("postId") String postId, @Param("postDto") TPostDto postDto);

    /**
     * 删除帖子
     * @param postId 帖子ID
     */
    @Delete("DELETE FROM t_post WHERE post_id = #{postId}")
    void deletePost(@Param("postId") String postId);
}
